<!-- 芯片标识符 -->
<template>
  <div class="app-container">
    <el-form
      ref="formDom"
      :model="model"
      :rules="fromField.rules"
      label-position="left"
      class="login-form"
    >
      <el-row :gutter="40">
        <el-col :span="8" v-for="item in fromField.field" :key="item.value">
          <el-form-item :label="item.paramName" :prop="item.paramKey">
            <el-input v-model="model[item.paramKey]" :placeholder="item.placeh" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="40">
        <el-col :span="6">
          <el-form-item label="安全芯片厂家授权书	" prop="authorizeFileOk">
            <el-upload
              class="avatar-uploader"
              action
              :show-file-list="false"
              :before-upload="beforeAuthorizeFileImgUpload"
            >
              <img
                v-if="model.authorizeFile.fileName"
                :src="'data:image/jpeg;base64,' + model.authorizeFile.value"
                class="avatar"
              />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item style="width:100%;">
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              style="width:100%;"
              @click.native.prevent="handleSubmit"
            >
              <span v-if="!loading">提交</span>
              <span v-else>提交...</span>
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import { Notification } from "element-ui";
import G6Api from "@/api/g6/g6Api";
import FromData from "./formModel";
export default {
  name: "getChipPrefix",
  data() {
    return {
      loading: false,
      model: {
        authorizeFileOk: null,
        authorizeFile: {
          type: "",
          fileName: "",
          value: ""
        }
      },
      fromField: FromData.getChipPrefixForm
    };
  },
  computed: {},
  mounted() {
    //this.insertVehicleType = data;
  },
  methods: {
    beforeAuthorizeFileImgUpload(file) {
      this.baseBeforeUpload(file, "authorizeFile");
    },
    handleSubmit() {
      this.$refs.formDom.validate(valid => {
        if (valid) {
          G6Api.getChipPrefix(this.model)
            .then(data => {
              Notification({
                message: "申请提交",
                type: "success",
                duration: 5 * 1000
              });
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        }
      });
    },

    baseBeforeUpload(file, nameField) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
        return false;
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
        return false;
      }
      const name = file.name;
      const nameArr = name.split(".");
      this.model[nameField].type = nameArr[nameArr.length - 1];
      this.model[nameField].fileName = nameArr.slice(-2).join(".");
      this.getImgToBase64(file, this.model[nameField]);
      this.model[nameField + "Ok"] = true;
      return false;
    },
    getImgToBase64(file, obj) {
      //将图片转换为Base64
      var reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onload = function(e) {
        obj.value = this.result.substring(this.result.indexOf(",") + 1);
      };
    }
  }
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
